package aip.assignment2.view.converter;

import aip.assignment2.persistence.entity.Country;
import aip.assignment2.service.PurchaseManager;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Named;

/**
 * This class is the converter for Country.
 */
@Named("aip.assignment2.view.converter.CountryConverter")
public class CountryConverter implements Converter, Serializable
{
	@EJB
	private PurchaseManager m_PurchaseManager;
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value)
	{
		try
		{
			return m_PurchaseManager.getCountryById(Integer.parseInt(value));
		}
		catch (Exception ex)
		{
			return null;
		}
	}
	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value)
	{
		try
		{
			return ((Country) value).getId().toString();
		}
		catch (Exception ex)
		{
			return null;
		}
	}
}
